package com.kodcu.web.component;

import com.kodcu.data.dao.interfaces.EpostaDao;
import com.kodcu.data.dao.jpa.EpostaDaoJPAImp;
import com.kodcu.data.dataobjects.Eposta;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.validation.validator.EmailAddressValidator;

import java.util.Date;

/**
 * Altug Bilgin ALTINTAS - altug@kodcu.com
 */
public class EpostaKayitPanel extends Panel {

    @SpringBean
    EpostaDao epostaDao;
            
    Eposta eposta = new Eposta();

    public EpostaKayitPanel(String id) {
        super(id);

        final FeedbackPanel feedbackPanel = new FeedbackPanel("feedbackPanel");
        feedbackPanel.setOutputMarkupPlaceholderTag(true);        
        add(feedbackPanel);

        Form frmEpostaKayit = new Form("frmEpostaKayit");
        add(frmEpostaKayit);

        TextField txtEposta = new TextField("txtEposta", new PropertyModel(eposta,"eposta"));
        txtEposta.setLabel(new ResourceModel("Eposta"));
        txtEposta.setRequired(true);
        txtEposta.add( EmailAddressValidator.getInstance());
        frmEpostaKayit.add(txtEposta);

        AjaxButton ajaxButton = new AjaxButton("ajaxButton") {
            @Override
            protected void onSubmit(AjaxRequestTarget ajaxRequestTarget, Form<?> form) {
                eposta.setTarih(new Date().toString()); 
                epostaDao.save(eposta) ;
                feedbackPanel.info(new ResourceModel("Eposta.kayit.basarili").getObject());
                ajaxRequestTarget.addComponent(feedbackPanel);
            }

            @Override
            protected void onError(AjaxRequestTarget target, Form<?> form) {
                target.addComponent(feedbackPanel);
            }
        } ;

        frmEpostaKayit.add(ajaxButton);

    }

}
